//
//  CYWenDaDetail_WenTiTableViewCell.m
//  cyydt
//
//  Created by chu on 2019/1/27.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYWenDaDetail_WenTiTableViewCell.h"

@implementation CYWenDaDetail_WenTiTableViewCell

+ (instancetype)initWithTableView:(UITableView *)tableView Identifier:(NSString *)identifier{
    CYWenDaDetail_WenTiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CYWenDaDetail_WenTiTableViewCell" owner:self options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)setModel:(CYProjectModel *)model{
    _model = model;
    
    if ([model.is_follow integerValue] == 1) {
        [self.guanzhuBtn setTitle:@"已关注" forState:UIControlStateNormal];
        self.guanzhuBtn.layer.borderColor = RGBOF(0xe3e3e3).CGColor;
        [self.guanzhuBtn setBackgroundColor:[UIColor whiteColor]];
        [self.guanzhuBtn setTitleColor:RGBOF(0x999999) forState:UIControlStateNormal];
    }else{
        [self.guanzhuBtn setTitle:@"+ 关注" forState:UIControlStateNormal];
        self.guanzhuBtn.layer.borderColor = RGBOF(0xFF5353).CGColor;
        [self.guanzhuBtn setBackgroundColor:RGBOF(0xFF5353)];
        [self.guanzhuBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];
    self.nicknameLabel.text = model.nickname;
    self.timeLabel.text = model.created_time;

    CGFloat titleHeight = [[Tool sharedInstance] heightForString:model.title andWidth:kWindowW - 30 fontSize:16];
    self.titleLabel.text = model.title;
    self.titleLabelHeightConstraint.constant = titleHeight;
    
    //回答描述
    NSString *content = @"";
    if (![NSString stringIsNull:model.content]) {
        content = model.content;
    }
    self.introLabel.text = [[Tool sharedInstance] delHTMLBiqoQianWithHtml:content];
    CGFloat introHeight = [[Tool sharedInstance] heightForString:[[Tool sharedInstance] delHTMLBiqoQianWithHtml:content] andWidth:kWindowW - 30 fontSize:12];
    NSArray *lineNum = [[Tool sharedInstance] getLinesArrayOfStringWithString:content Font:[UIFont systemFontOfSize:12] Rect:CGRectMake(0, 0, kWindowW - 30, MAXFLOAT)];
    if (lineNum.count == 1) {
        introHeight = 15;
        self.openBtnHeightConstraint.constant = 0;
        self.openBtnMarginTopConstraint.constant = 0;
        self.introLabelHeightConstraint.constant = introHeight;
        self.openBtn.hidden = YES;
    }else if (lineNum.count == 2){
        introHeight = 30;
        self.openBtnHeightConstraint.constant = 0;
        self.openBtnMarginTopConstraint.constant = 0;
        self.introLabelHeightConstraint.constant = introHeight;
        self.openBtn.hidden = YES;
    }else if (!lineNum.count){
        introHeight = 0;
        self.openBtnHeightConstraint.constant = 0;
        self.openBtnMarginTopConstraint.constant = 0;
        self.introLabelHeightConstraint.constant = introHeight;
        self.openBtn.hidden = YES;
    }else{
        if ([model.isOpen boolValue]) {
            self.openBtnHeightConstraint.constant = 20;
            self.openBtnMarginTopConstraint.constant = 10;
            self.introLabelHeightConstraint.constant = introHeight;
            self.openBtn.hidden = NO;
            [self.openBtn setTitle:@"收起" forState:UIControlStateNormal];
        }else{
            introHeight = 30;
            self.openBtnHeightConstraint.constant = 20;
            self.openBtnMarginTopConstraint.constant = 10;
            self.introLabelHeightConstraint.constant = introHeight;
            self.openBtn.hidden = NO;
            [self.openBtn setTitle:@"展开" forState:UIControlStateNormal];
        }
    }
    
    if (model.img_url.count > 0) {
        self.imagesViewHeightConstraint.constant = (kWindowW - 60) / 3;
        self.imagesViewMarginTopConstraint.constant = 15;
        NSInteger count = model.img_url.count > 3 ? 3 : model.img_url.count;
        for (int i = 0; i < count; i++) {
            NSString *url = model.img_url[i];
            UIImageView *imageView = self.imageViews[i];
            [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];

        }
    }else{
        self.imagesViewHeightConstraint.constant = 0;
        self.imagesViewMarginTopConstraint.constant = 0;
    }
    
    
    if (model.answer_number) {
        self.label1.text = [NSString stringWithFormat:@"%@个回答", model.answer_number];
    }else{
        self.label1.text = @"0个回答";
    }
    if (model.collect_number) {
        self.label2.text = [NSString stringWithFormat:@"%@收藏", model.collect_number];
    }else{
        self.label2.text = @"0收藏";
    }
    
    [self layoutIfNeeded];
    self.cellHeight = CGRectGetMaxY(self.label1.frame) + 20;
}

- (IBAction)openAction:(UIButton *)sender {
    if (self.block) {
        self.block();
    }
}

- (IBAction)guanzhuAction:(UIButton *)sender {
    if (self.guanzhuBlock) {
        self.guanzhuBlock();
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.headImageView.layer.cornerRadius = self.headImageView.frame.size.height / 2;
    self.headImageView.layer.masksToBounds = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeadImageView:)];
    [self.headImageView addGestureRecognizer:tap];
    self.headImageView.userInteractionEnabled = YES;
    
    self.guanzhuBtn.layer.cornerRadius = 5;
    self.guanzhuBtn.layer.masksToBounds = YES;
    self.guanzhuBtn.layer.borderWidth = 1;
}

- (void)tapHeadImageView:(UITapGestureRecognizer *)tap{
    CYGeRenZhuYeViewController *zhuye = [[CYGeRenZhuYeViewController alloc] init];
    if (self.model.user_id) {
        zhuye.userId = self.model.user_id;
    }else{
        zhuye.userId = self.model.userId;
    }
    [[AppDelegate sharedAppDelegate] pushViewController:zhuye];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
